﻿using System.Collections.Generic;
using System.IO;

namespace GT.Algorithms
{
	public class IDF
	{
		public Dictionary<string, NodeX> Nodes { get; set; }
		public Dictionary<string, Dictionary<string, NodeX>> NodesX { get; set; }
		public string Path { get; set; }
		public IDF(string path)
		{
			Path = path;
			Nodes = new Dictionary<string, NodeX>();
			NodesX = new Dictionary<string, Dictionary<string, NodeX>>();
			using (StreamReader sr = File.OpenText(path))
			{
				string x = sr.ReadLine();;

				while (!string.IsNullOrEmpty(x))
				{	
					string[] xx = x.Split(' ');
					NodeX n = new NodeX(xx[0], xx[1], xx[2]);
					Nodes.Add(xx[0], n);
					if (!NodesX.ContainsKey(xx[2]))
					{
						NodesX.Add(xx[2], new Dictionary<string, NodeX>());
					}
					NodesX[xx[2]].Add(n.NodeText, n);
					x = sr.ReadLine();
				} 
			}
		}
		public bool Search(string x)
		{
			string[] xs = x.Split(' ');
			NodeX nx = Nodes[GST.ROOT_ID.ToLower()];
			int i = 0;
			bool stop = false;
			while (!stop && i < xs.Length)
			{
				Dictionary<string, NodeX> nodes = NodesX[nx.NodeID];
				if (nodes.Count == 0) break;
				if (nodes.ContainsKey(xs[i]))
				{
					nx = nodes[xs[i]];
					i++;
					stop = i == xs.Length - 1;
				}
				else stop = true;
// 				for (int j = 0; j < nodes.Count; j++)
// 				{
// 					
// 					else
// 					{
// 						stop = j == nodes.Count - 1;
// 					}
// 				}
			}
			return i == xs.Length;
		}
	}
	public class NodeX
	{
		public string NodeID { get; set; }
		public string NodeText { get; set; }
		public string ParentID { get; set; }
		public NodeX(string nodeID, string nodeText, string parentID)
		{
			NodeID = nodeID;
			NodeText = nodeText;
			ParentID = parentID;
		}
	}
}
